package com.ropeok.controller;

import com.ropeok.common.ResponseResult;
import com.ropeok.entity.User;
import com.ropeok.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.spi.ResolveResult;
import java.util.List;

/**
 * @BelongsProject: training-mission
 * @BelongsPackage: com.ropeok.controller
 * @Author: zhofeifan
 * @CreateTime: 2022-08-29  10:23
 * @Description: User的Controller层
 * @Version: 1.0
 */
//@Controller
//@RespectBinding

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @description:
     * @author: zhoufeifan
     * @date: 2022/8/29 13:52
     * @param: [long]
     * @return: com.ropeok.common.ResponseResult
     **/
    @GetMapping("/user/{id}")
    public ResponseResult findById(@PathVariable long id) {
        User user = userService.findById(id);
        if (user == null) {
            //说明没有改用户
            return new ResponseResult(444, "没有改用户");
        }
        return new ResponseResult(666, "操作成功", user);
    }

    /**
     * @description:
     * @author: zhoufeifan
     * @date: 2022/8/29 14:14
     * @param: []
     * @return: com.ropeok.common.ResponseResult
     **/
    @GetMapping("/user")
    public ResponseResult findAll() {
        List<User> list = userService.getUserList();
        return new ResponseResult(666, "操作成功", list);
    }

    /**
     * @description:
     * @author: zhoufeifan
     * @date: 2022/8/29 14:17
     * @param: [com.ropeok.entity.User]
     * @return: com.ropeok.common.ResponseResult
     **/
    @PostMapping("/user")
    public ResponseResult insertUser(@RequestBody User user) {
        userService.addUser(user);
        return new ResponseResult(666, "操作成功");
    }

    /**
     * @description:
     * @author: zhoufeifan
     * @date: 2022/8/29 14:21
     * @param: [java.lang.Integer]
     * @return: com.ropeok.common.ResponseResult
     **/
    @DeleteMapping("/user/{id}")
    public ResponseResult deleteUser(@PathVariable("id") Integer id) {
        userService.deleteUser(id);
        return new ResponseResult(666, "操作成功");
    }

    /**
     * @description:
     * @author: zhoufeifan
     * @date: 2022/8/29 14:21
     * @param: [com.ropeok.entity.User]
     * @return: com.ropeok.common.ResponseResult
     **/
    @PutMapping("/user")
    public ResponseResult updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return new ResponseResult(666, "操作成功");
    }
}
